home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0036_Base Notation.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-21  |  3KB  |  63 lines

  1.  
  2. { How about a procedure that will display any integer in any base
  3.  notation from 2 to 16?  The following example displays the values
  4.  0 through 15 in binary (base 2), octal (base 8), decimal (base 10)
  5.  and hexadecimal (base 16) notations ... }
  6.  
  7. (********************************************************************)
  8. PROGRAM BaseX;                      (* compiler: Turbo Pascal v4.0+ *)
  9.                                     (* Nov.14.93 Greg Vigneault     *)
  10. (*------------------------------------------------------------------*)
  11. (* Display any INTEGER in any base notation from 2 to 16...         *)
  12. (*                                                                  *)
  13. (*    number base 2  = binary notation       (digits 0,1)           *)
  14. (*    number base 8  = octal notation        (digits 0..7)          *)
  15. (*    number base 10 = decimal notation      (digits 0..9)          *)
  16. (*    number base 16 = hexadecimal notation  (digits 0..9,A..F)     *)
  17.  
  18. PROCEDURE DisplayInteger (AnyInteger :INTEGER; NumberBase :BYTE);
  19.   CONST DataSize = 16;  (* bit-size of an INTEGER *)
  20.   VAR   Index : INTEGER;
  21.         Digit : ARRAY [1..DataSize] OF CHAR;
  22.   BEGIN
  23.     IF (NumberBase > 1) AND (NumberBase < 17) THEN BEGIN
  24.       Index := 0;
  25.       REPEAT
  26.         INC (Index);
  27.         Digit [Index] := CHR(AnyInteger MOD NumberBase + ORD('0'));
  28.         IF (Digit [Index] > '9') THEN INC (Digit [Index],7);
  29.         AnyInteger := AnyInteger DIV NumberBase;
  30.       UNTIL (AnyInteger = 0) OR (Index = DataSize);
  31.       WHILE (Index > 0) DO BEGIN
  32.         Write (Digit [Index]);
  33.         DEC (Index);
  34.       END; {WHILE Index}
  35.     END; {IF NumberBase}
  36.   END {DisplayInteger};
  37.  
  38. (*------------------------------------------------------------------*)
  39. (*  to test the DisplayInteger procedure...                         *)
  40.  
  41. VAR Base, Number : INTEGER;
  42.  
  43. BEGIN
  44.       FOR Base := 2 TO 16 DO
  45.         CASE Base OF
  46.           2,8,10,16 : BEGIN
  47.                         WriteLn;
  48.                         CASE Base OF
  49.                           2  : Write ('Binary : ');
  50.                           8  : Write ('Octal  : ');
  51.                           10 : Write ('Decimal: ');
  52.                           16 : Write ('Hex    : ');
  53.                         END; {CASE}
  54.                         FOR Number := 0 TO 15 DO BEGIN
  55.                           DisplayInteger (Number, Base);
  56.                           Write (' ');
  57.                         END; {FOR}
  58.                       END;
  59.         END; {CASE}
  60.       WriteLn;
  61.  
  62. END {BaseX}.
  63.